package com.tssup.fesco.service.impl;

import com.tssup.fesco.common.exeception.RetException;
import com.tssup.fesco.common.utils.json.JsonUtil;
import com.tssup.fesco.common.constants.ReturnCodeConstants;
import com.tssup.fesco.common.enumeration.PayWay;
import com.tssup.fesco.common.enumeration.PointTradeType;
import com.tssup.fesco.common.enumeration.PointType;
import com.tssup.fesco.service.UserPointService;
import com.tssup.fesco.ofc.vo.OperationPointParameter;
import com.tssup.fesco.sales.order.persistent.mapper.IOrderMapper;
import com.tssup.fesco.sales.order.persistent.pojo.IOrder;
import com.tssup.fesco.sales.organization.persistent.pojo.IOrganizationPoint;
import com.tssup.fesco.sales.organization.persistent.pojo.IOrganizationPointDetail;
import com.tssup.fesco.sales.user.persistent.mapper.*;
import com.tssup.fesco.sales.user.persistent.pojo.IUser;
import com.tssup.fesco.sales.user.persistent.pojo.IUserPoint;
import com.tssup.fesco.sales.user.persistent.pojo.IUserPointDetail;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author haomin
 * @date 2016年7月31日
 * @see
 * @version
 */
@Service
public class UserPointServiceImpl implements UserPointService {

    private static Logger log = LoggerFactory.getLogger(UserPointServiceImpl.class);

    @Autowired
    private IOrderMapper orderMapper;

    @Autowired
    private IUserPointMapper userPointMapper ;

    @Autowired
    private IOrganizationPointMapper organizationPointMapper;

    @Autowired
    private IUserMapper userMapper;

    @Autowired
    private IUserPointDetailMapper userPointDetailMapper;

    @Autowired
    private IOrganizationPointDetailMapper organizationPointDetailMapper;

    /**
     * 操作积分
     * @param parameter
     */
    @Override
    @Transactional
    public void operationPoint(OperationPointParameter parameter) {
        log.info("【积分操作 operationPoint】-----入参-----"+ JsonUtil.toJson(parameter));
        if(StringUtils.isNotEmpty(parameter.getOrderCode())){
            IOrder order = orderMapper.selectByOrderCode(parameter.getOrderCode());
            parameter.setOrderId(order.getOrderId());
        }
        if (null != parameter && null != parameter.getPointType() && null != parameter.getPointTradeType()){
            if ( PointType.PERSONAL.equals(parameter.getPointType())){
                IUserPoint iUserPoint = userPointMapper.selectByUserId(parameter.getUserId());
                BigDecimal currentPoint = iUserPoint.getPoint();
                if ( PointTradeType.ADD.equals(parameter.getPointTradeType())){
                    iUserPoint.setPoint(currentPoint.add(parameter.getPoint()));
                    if (null != parameter.getRechargeDate()) {
                        iUserPoint.setLastUpdateTime(parameter.getRechargeDate());
                    }
                }
                if (PointTradeType.MINUS.equals(parameter.getPointTradeType())){
                    if (1 == parameter.getPoint().compareTo(currentPoint)){
                        throw new RetException(ReturnCodeConstants.LACK_POINT_FAIL_CODE, ReturnCodeConstants.LACK_POINT_FAIL_MSG);
                    }
                    iUserPoint.setPoint(currentPoint.subtract(parameter.getPoint()));
                }
                userPointMapper.updateByPrimaryKey(iUserPoint);
                savUserPointDetail(iUserPoint.getPointId(), currentPoint, parameter);
            } else if (PointType.ORGANIZATION.equals(parameter.getPointType())){
                IOrganizationPoint iOrganizationPoint = organizationPointMapper.selectByOrganizationId(parameter.getOrganizationId());
                BigDecimal currentPoint = iOrganizationPoint.getPoint();
                if (PointTradeType.ADD.equals(parameter.getPointTradeType())){
                    iOrganizationPoint.setPoint(currentPoint.add(parameter.getPoint()));
                    if (null != parameter.getRechargeDate()) {
                        iOrganizationPoint.setLastUpdateTime(parameter.getRechargeDate());
                    }
                }
                if (PointTradeType.MINUS.equals(parameter.getPointTradeType())){
                    if (1 == parameter.getPoint().compareTo(currentPoint)){
                        throw new RetException(ReturnCodeConstants.LACK_POINT_FAIL_CODE, ReturnCodeConstants.LACK_POINT_FAIL_MSG);
                    }
                    iOrganizationPoint.setPoint(currentPoint.subtract(parameter.getPoint()));
                }
                organizationPointMapper.updateByPrimaryKey(iOrganizationPoint);
                saveOrganizationPointDetail(iOrganizationPoint.getPointId(), currentPoint, parameter);
            }
        }
    }

    private void savUserPointDetail(Integer pointId, BigDecimal currentPoint, OperationPointParameter operationPointParameter){
        if (StringUtils.isNotEmpty(operationPointParameter.getOrderCode()) && null != operationPointParameter.getOrderId() ){
            IOrder order = orderMapper.selectByPrimaryKey(operationPointParameter.getOrderId());
            if (null != order) {
                operationPointParameter.setOrderCode(order.getOrderCode());
            }
        }

        IUserPointDetail userPointDetail = new IUserPointDetail() ;
        userPointDetail.setOperator(operationPointParameter.getUserId());
        IUser iUser = userMapper.selectByPrimaryKey(operationPointParameter.getUserId());
        if (PointTradeType.MINUS.equals(operationPointParameter.getPointTradeType())){
            //积分支付
            userPointDetail.setPaywayId(PayWay.POINT.getPayWayId());
            //消费
            userPointDetail.setTradeType(2);
        }
        if (PointTradeType.ADD.equals(operationPointParameter.getPointTradeType()) && null == operationPointParameter.getOfcOrderCode() ){
            //微信支付
            userPointDetail.setPaywayId(PayWay.WECHAT.getPayWayId());
            //个人充值
            userPointDetail.setTradeType(1);
        }
        if (PointTradeType.ADD.equals(operationPointParameter.getPointTradeType()) && null != operationPointParameter.getOfcOrderCode() ){
            //积分退回
            userPointDetail.setPaywayId(PayWay.POINT_REFUND.getPayWayId());
            //退款
            userPointDetail.setTradeType(4);
        }
        userPointDetail.setOperatorName(iUser.getChineseName());
        userPointDetail.setOrderId(operationPointParameter.getOrderId());
        userPointDetail.setOrderCode(operationPointParameter.getOrderCode());
        userPointDetail.setUserPayId(operationPointParameter.getUserPayId());
        userPointDetail.setPayCode(operationPointParameter.getPayCode());
        userPointDetail.setPointId(pointId);
        userPointDetail.setPoint(operationPointParameter.getPoint());
        userPointDetail.setCurrentPoint(currentPoint);
        userPointDetail.setCreateDate(new Date());
        userPointDetailMapper.insert(userPointDetail);
    }

    private void saveOrganizationPointDetail(Integer pointId, BigDecimal currentPoint, OperationPointParameter operationPointParameter){
        IOrganizationPointDetail organizationPointDetail = new IOrganizationPointDetail() ;
        organizationPointDetail.setPaywayId(PayWay.WECHAT.getPayWayId());
        if (StringUtils.isNotEmpty(operationPointParameter.getOrderCode()) && null != operationPointParameter.getOrderId() ){
            IOrder order = orderMapper.selectByPrimaryKey(operationPointParameter.getOrderId());
            if (order != null) {
                operationPointParameter.setOrderCode(order.getOrderCode());
                organizationPointDetail.setPaywayId(PayWay.POINT.getPayWayId());
            }
        }
        organizationPointDetail.setOperator(operationPointParameter.getUserId());
        IUser iUser = userMapper.selectByPrimaryKey(operationPointParameter.getUserId());
        organizationPointDetail.setOperatorName(iUser.getChineseName());
        organizationPointDetail.setOrderId(operationPointParameter.getOrderId());
        organizationPointDetail.setOrderCode(operationPointParameter.getOrderCode());
        organizationPointDetail.setTradeType(operationPointParameter.getPointTradeType().getValue());
        if (PointTradeType.MINUS.equals(operationPointParameter.getPointTradeType())){
            //积分支付
            organizationPointDetail.setPaywayId(PayWay.POINT.getPayWayId());
            //积分消费
            organizationPointDetail.setTradeType(2);
        }
        if (PointTradeType.ADD.equals(operationPointParameter.getPointTradeType()) && null == operationPointParameter.getOfcOrderCode() ){
            //微信支付
            organizationPointDetail.setPaywayId(PayWay.WECHAT.getPayWayId());
            //线上充值
            organizationPointDetail.setTradeType(1);
        }
        if (PointTradeType.ADD.equals(operationPointParameter.getPointTradeType()) && null != operationPointParameter.getOfcOrderCode() ){
            //积分退回
            organizationPointDetail.setPaywayId(PayWay.POINT_REFUND.getPayWayId());
            //退款
            organizationPointDetail.setTradeType(4);
        }

        organizationPointDetail.setUserPayId(operationPointParameter.getUserPayId());
        organizationPointDetail.setPayCode(operationPointParameter.getPayCode());
        organizationPointDetail.setPointId(pointId);
        organizationPointDetail.setPoint(operationPointParameter.getPoint());
        organizationPointDetail.setCurrentPoint(currentPoint);
        organizationPointDetail.setCreateDate(new Date());
        organizationPointDetail.setOrganizationId(operationPointParameter.getOrganizationId());
        organizationPointDetailMapper.insert(organizationPointDetail);
    }
}
